package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.util.AuthenticationUtil;
import com.cskaoyan.util.TransactionUtil;
import lombok.SneakyThrows;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author stone
 * @date 2023/03/18 02:15
 * 一般日志：用户觉得需要查看的一般操作日志，建议是默认的日志级别
 * 安全日志：用户安全相关的操作日志，例如登录、删除管理员
 * 订单日志：用户交易相关的操作日志，例如订单发货、退款
 * 其他日志：如果以上三种不合适，可以选择其他日志，建议是优先级最低的日志级别
 */
public class LogServiceImpl implements LogService {
    public static final  Integer LOG_TYPE_GENERAL = 0;
    public static final  Integer LOG_TYPE_AUTH = 1;
    public static final  Integer LOG_TYPE_ORDER = 2;
    public static final  Integer LOG_TYPE_OTHER = 3;
    @Override
    public void logOrderSucceed(String action, String result) {
        logAdmin(LOG_TYPE_ORDER, action, true, result, "");
    }

    @SneakyThrows
    public void logAdmin(Integer type, String action, Boolean succeed, String result, String comment) {
        MarketLog log = new MarketLog();
        MarketAdmin admin = (MarketAdmin) AuthenticationUtil.getPrincipal();
        if (admin != null) {
            log.setAdmin(admin.getUsername());
        } else {
            log.setAdmin("匿名用户");
        }

        //ip就先不放了
        log.setType(type);
        log.setAction(action);
        log.setStatus(succeed);
        log.setResult(result);
        log.setComment(comment);
        Date now = new Date();
        log.setAddTime(now);
        log.setUpdateTime(now);
        MarketLogMapper logMapper = TransactionUtil.getMapper(MarketLogMapper.class);
        logMapper.insertSelective(log);
    }
}
